package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.House;
import com.qf.enums.AppHttpCodeEnum;
import com.qf.exception.SystemException;
import com.qf.mapper.HouseMapper;
import com.qf.result.ResponseResult;
import com.qf.service.HouseService;
import com.qf.utils.BeanCopyUtils;
import com.qf.vo.HouseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (House)表服务实现类
 *
 * @author makejava
 * @since 2023-02-04 22:30:18
 */
@Service("houseService")
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {

    @Override
    public ResponseResult getHouseList() {
        List<House> houses = list();
        List<HouseVo> houseVos = BeanCopyUtils.copyBeanList(houses, HouseVo.class);
        return ResponseResult.okResult(houseVos);
    }

    @Override
    public ResponseResult addHouse(House house) {
        house.setHouseStatus("0");
        boolean flag = save(house);
        if (flag)
            return ResponseResult.okResult();
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult updateHouse(House house) {
        if (getById(house.getId()) == null) {
            throw new SystemException(AppHttpCodeEnum.ID_ERROR);
        }
        house.setHouseStatus("0");
        boolean flag = updateById(house);
        if (flag)
            return ResponseResult.okResult();
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult deleteHouse(Integer id) {
        if (getById(id) == null) {
            throw new SystemException(AppHttpCodeEnum.ID_ERROR);
        }
        if (getById(id).getOwnId() != null) {
            throw new SystemException(AppHttpCodeEnum.HOUSE_ERROR);
        }
        boolean flag = removeById(id);
        if (flag)
            return ResponseResult.okResult();
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult getHouseByStatus() {
        //获取所有未分配的房屋列表
        LambdaQueryWrapper<House> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(House::getHouseStatus, 0)
                .select(House::getId, House::getHouseNum);
        List<House> list = list(queryWrapper);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult getHouseByTrueStatus() {
        //获取所有已分配的房屋列表
        LambdaQueryWrapper<House> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(House::getHouseStatus, 0)
                .select(House::getId, House::getHouseNum);
        List<House> list = list(queryWrapper);
        return ResponseResult.okResult(list);
    }
}
